/**
 * 一些对原生js 对象的的方法扩展
 */



/**
字符串初格式化方法
如 ”{0} hello {1}“.format("hi","ken")
输出 hi hello ken
*/
String.prototype.format = function(args) {
    var result = this;
    if (arguments.length > 0) {    
        if (arguments.length == 1 && typeof (args) == "object") {
            for (var key in args) {
                args[key]=(args[key]==undefined||args[key]==null)?"":args[key];
                if(args[key]!=undefined){
                    var reg = new RegExp("({" + key + "})", "g");
                    result = result.replace(reg, args[key]);
                }
            }
        }
        else {
            for (var i = 0; i < arguments.length; i++) {
                arguments[i]=(arguments[i]==undefined||arguments[i]==null)?"":arguments[i];
                if (arguments[i] != undefined) {
　　　　　　　　　　　　var reg= new RegExp("({)" + i + "(})", "g");
                    result = result.replace(reg, arguments[i]);
                }
            }
        }
    }
    return result;
}

/**
 * 字符串替换全部
 * @param  {[type]} s1 [description]
 * @param  {[type]} s2 [description]
 * @return {[type]}    [description]
 */
String.prototype.replaceAll  = function(s1,s2){   
    return this.replace(new RegExp(s1,"gm"),s2);   
} 

/**
 * 日期类的扩展
 */
//日期添加
Date.prototype.add = function(milliseconds){
    var m = this.getTime() + milliseconds;
    return new Date(m);
};
//添加秒
Date.prototype.addSeconds = function(second){
    return this.add(second * 1000);
};
//添加分钟
Date.prototype.addMinutes = function(minute){
    return this.addSeconds(minute*60);
};
//添加小时
Date.prototype.addHours = function(hour){
    return this.addMinutes(60*hour);
};
//添加天
Date.prototype.addDays = function(day){
    return this.addHours(day * 24);
};
//添加年
Date.prototype.addYears = function(year){
    return new Date(this.getFullYear() + year,this.getMonth(),this.getDate(),this.getHours(),this.getMinutes(),this.getSeconds());
};
//添加月
Date.prototype.addMonth = function(){
    var m = this.getMonth();
    if(m == 11)return new Date(this.getFullYear() + 1,1,this.getDate(),this.getHours(),this.getMinutes(),this.getSeconds());
    
    var daysInNextMonth = Date.daysInMonth(this.getFullYear(),this.getMonth() + 1);
    var day = this.getDate();
    if(day > daysInNextMonth){
        day = daysInNextMonth;
    }
    return new Date(this.getFullYear(),this.getMonth() + 1,day,this.getHours(),this.getMinutes(),this.getSeconds());    
};
//是否闰年
Date.prototype.isLeepYear = function(year){
    return (year % 4 == 0 && year % 100 != 0)
};
//某年某月有多少天
Date.prototype.daysInMonth = function(year,month){
    if(month == 2){
        if(year % 4 == 0 && year % 100 != 0)
            return 29;
        else
            return 28;
    }
    else if((month <= 7 && month % 2 == 1) || (month > 7 && month % 2 == 0))
        return 31;
    else
        return 30;
};
Date.prototype.formatDate = function(format) {
  var o = {
    "M+": this.getMonth() + 1, //month
    "d+": this.getDate(), //day
    "h+": this.getHours(), //hour
    "m+": this.getMinutes(), //minute
    "s+": this.getSeconds(), //second
    "q+": Math.floor((this.getMonth() + 3) / 3), //quarter
    "S": this.getMilliseconds() //millisecond
  };
  if (/(y+)/.test(format)) 
  {
        format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  }
  for (var k in o)
  {
        if (new RegExp("(" + k + ")").test(format))
        {
            format = format.replace(RegExp.$1,
            RegExp.$1.length == 1 ? o[k] :
            ("00" + o[k]).substr(("" + o[k]).length));
        }
  } 
  return format;
};
Date.prototype.format = Date.prototype.formatDate;
//只能转换2016-6-2  这种非标准的格式为 date
Date.prototype.convert=function(dateStr){
  if(!dateStr){
    return ;
  }
  var dsp=dateStr.indexOf("-")>-1,dstr=[];
  if(dsp)
  {
    dstr=dateStr.split("-");
  }
  else
  {
    //判断是否满足8位
    if(dateStr.length==8){
        dstr.push(dateStr.substring(0,4));
        dstr.push(dateStr.substring(4,6));
        dstr.push(dateStr.substring(6,8));
    }
  }
  
  var year=dstr[0],month=dstr[1],day=dstr[2];
  month=month.length==1?("0"+(parseInt(month)-1)):month.indexOf("0")==0?("0"+(parseInt(month)-1)):(parseInt(month)-1);
  day=day.length==1?("0"+day):day;
  this.setYear(year);
  this.setMonth(month);
  this.setDate(day);

  return this;
};

